package com.wyd.jwpkglxt_ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.jwpkglxt_ssm.entity.OpeningClassInfo;
import com.wyd.jwpkglxt_ssm.dao.OpeningClassInfoDao;
import com.wyd.jwpkglxt_ssm.service.OpeningClassInfoService;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (OpeningClassInfo)表服务实现类
 *
 * @author wyd
 * @since 2021-08-12 12:58:10
 */
@Service("openingClassInfoService")
public class OpeningClassInfoServiceImpl implements OpeningClassInfoService {
    @Autowired
    private OpeningClassInfoDao openingClassInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OpeningClassInfo queryById(Integer id) {
        return this.openingClassInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<OpeningClassInfo> queryAllByLimit(int offset, int limit) {
        return this.openingClassInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param openingClassInfo 实例对象
     * @return 实例对象
     */
    @Override
    public OpeningClassInfo insert(OpeningClassInfo openingClassInfo) {
        //Date openingDate = openingClassInfo.getOpeningDate();
        //Date endingDate = openingClassInfo.getEndingDate();
        //List<OpeningClassInfo> openingClassInfos = this.openingClassInfoDao.queryAll(new OpeningClassInfo().setClassroomId(openingClassInfo.getClassroomId()));
        //if(!openingClassInfos.isEmpty()){
        //    for(int i=0;i<openingClassInfos.size();i++){
        //        Date openingDate1 = openingClassInfos.get(i).getOpeningDate();
        //        Date endingDate1 = openingClassInfos.get(i).getEndingDate();
        //        //compareTo()方法的返回值，openingDate小于openingDate1返回-1，openingDate大于openingDate1返回1，相等返回0
        //        int i1 = openingDate.compareTo(openingDate1);
        //        //开班时间比已有开班时间小，结束时间比已有结束时间小，但比已有开班时间大
        //        int i2 = endingDate.compareTo(endingDate1);
        //
        //    }
        //}
        this.openingClassInfoDao.insert(openingClassInfo.setCreateTime(new Timestamp(System.currentTimeMillis())));
        return openingClassInfo;
    }

    /**
     * 修改数据
     *
     * @param openingClassInfo 实例对象
     * @return 实例对象
     */
    @Override
    public OpeningClassInfo update(OpeningClassInfo openingClassInfo) {
        this.openingClassInfoDao.update(openingClassInfo);
        return this.queryById(openingClassInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.openingClassInfoDao.deleteById(id) > 0;
    }

    @Override
    public PageInfo<OpeningClassInfo> queryAllByPage(Integer pageNum, Integer pageSize, OpeningClassInfo openingClassInfo) {
        PageHelper.startPage(pageNum,pageSize);
        List<OpeningClassInfo> openingClassInfos = this.openingClassInfoDao.queryAllWithClassroomAndAreaAndProfessionTitle(openingClassInfo);
        return new PageInfo<>(openingClassInfos);
    }

    @Override
    public boolean deleteOpeningClassInfoBatch(String openingClassInfoIds) {
        if(openingClassInfoIds.contains("-")){
            String[] idsplit = openingClassInfoIds.split("-");
            List<Integer> Ids = new ArrayList<>();
            for(String s : idsplit){
                Ids.add(Integer.parseInt(s));
            }
            int delCourseSystems = this.openingClassInfoDao.deleteOpeningClassInfoBatch(Ids);
            return delCourseSystems != 0;
        }else if(!"".equals(openingClassInfoIds)){
            Integer id = Integer.parseInt(openingClassInfoIds);
            int delCourseSystem = this.openingClassInfoDao.deleteById(id);
            return delCourseSystem != 0;
        }
        return false;
    }
}